<?php
  class DistrictService{
    	
  	function getDistrictById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectProvince){
  		if(in_array($selectProvince, $provinceAccessId)){
  		
				$where = array();
				foreach($districtAccessId as $id){
					$where[] = " pn_degis_contact.aco_district_id = ".$id;
				}
				$whereImplode = implode(' OR ', $where);
				
				$sql = "
								 SELECT 						 
									pn_degis_district.adc_id,						 
									pn_degis_district.adc_name,
									pn_degis_district.adc_lat,
									pn_degis_district.adc_lon,						 							 	
									COUNT(pn_degis_contact.aco_district_id) AS count_population 							
								 FROM 
									samuidemap.pn_degis_district,
									samuidemap.pn_degis_contact
								WHERE  
								( 
									$whereImplode
								)
								AND
									pn_degis_contact.aco_province_id = $selectProvince
								AND 
									pn_degis_contact.aco_district_id = pn_degis_district.adc_id 										
								GROUP BY 
									pn_degis_contact.aco_district_id
								ORDER BY 
									pn_degis_district.adc_id";
		
					//echo $sql."<br><br>";
					$column = array('id', 'name', 'lat', 'lon','count_population');
					$result = DBUtil::executeSQL($sql);
					$objectArray = DBUtil::marshallObjects ($result, $column);
		
		
					//print_r($objectArray);
					
					return $objectArray; 
				//$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray);
    	}else{
				echo '<?xml version="1.0" encoding="UTF-8"?>
							<datas>
								<data id=0>
									<title>Permission denied , you cannot access this province.</title>
								</data>
							</datas>';     	
    	}
  	
  	}

/*
		function getDistrictByProvinceAccessId($uid, $provinceAccessId, $districtAccessId, $tambonAccessId){

			//Generate where condition
			$where = array();
			foreach($districtAccessId as $id){
				$where[] = " pn_degis_contact.aco_district_id = ".$id;
			}			
			
			$whereImplode = implode(' OR ', $where);
			
			$sql = "
							 SELECT 
								pn_degis_district.adc_province_id,								 						 
								pn_degis_district.adc_id,						 
								pn_degis_district.adc_name,
								pn_degis_district.adc_lat,
								pn_degis_district.adc_lon,						 							 	
								COUNT(pn_degis_contact.aco_district_id) AS count_population 							
							 FROM 
								samuidemap.pn_degis_district,
								samuidemap.pn_degis_contact
							WHERE  
							( 
								$whereImplode
							)
							AND 
								pn_degis_contact.aco_district_id = pn_degis_district.adc_id 										
							GROUP BY 
								pn_degis_contact.aco_district_id
							ORDER BY 
								pn_degis_district.adc_id";
	
				$column = array('pro_id', 'id', 'name', 'lat', 'lon','count_population');
				$result = DBUtil::executeSQL($sql);
				$objectArray = DBUtil::marshallObjects ($result, $column);
				
				return $objectArray;	
		}
*/  	
  	function arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray){
  	
			$accessArray['province_access'] = implode(",", $provinceAccessId);
			$accessArray['district_access'] = implode(",", $districtAccessId);
			$accessArray['tambon_access']   = implode(",", $tambonAccessId);
          
      //Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;
      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );
      foreach($objectArray as $val){
        $districtId = "dt".$val[id];
        $xml_node_data = $xml->createElement("data");
        $xml_node_datas->appendChild( $xml_node_data );
        $xml_node_data->setAttribute("id", $districtId);
          $xml_node_title = $xml->createElement("title",$val[name]);
          $xml_node_data->appendChild( $xml_node_title );
          $xml_node_population = $xml->createElement("population",$val[count_population]);
          $xml_node_data->appendChild( $xml_node_population );
          $xml_node_latitude = $xml->createElement("latitude",$val[lat]);
          $xml_node_data->appendChild( $xml_node_latitude );
          $xml_node_longitude = $xml->createElement("longitude",$val[lon]);
          $xml_node_data->appendChild( $xml_node_longitude );
          $xml_node_span = $xml->createElement("span","0.3");
          $xml_node_data->appendChild( $xml_node_span );
          $xml_node_infopath = $xml->createElement("infopath","information.xml");
          $xml_node_data->appendChild( $xml_node_infopath );
          $xml_node_reportpath = $xml->createElement("reportpath","http://data.votethailand.com/report_no_data.xml");
          $xml_node_data->appendChild( $xml_node_reportpath );
          $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=tambon&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;sd=".$val[id];
                    
          $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);//next xml
          $xml_node_data->appendChild( $xml_node_xmlpath );
          $xml_node_xmlupdate = $xml->createElement("xmlupdate","DistrictData");
          $xml_node_data->appendChild( $xml_node_xmlupdate );
          $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
          $xml_node_data->appendChild($xml_node_date);
      }

			return $xml;   

  	}
  	
  	
  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	}  	

  }  
?>